home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH3 / SRC / TOGRAY.FRM < prev    next >
Text File  |  1996-01-24  |  6KB  |  219 lines

  1. VERSION 4.00
  2. Begin VB.Form ToGrayForm 
  3.    Caption         =   "PalEdit"
  4.    ClientHeight    =   2550
  5.    ClientLeft      =   2595
  6.    ClientTop       =   2265
  7.    ClientWidth     =   3150
  8.    Height          =   2955
  9.    Left            =   2535
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   170.439
  12.    ScaleMode       =   0  'User
  13.    ScaleWidth      =   210
  14.    Top             =   1920
  15.    Visible         =   0   'False
  16.    Width           =   3270
  17.    Begin VB.PictureBox ImagePict 
  18.       AutoRedraw      =   -1  'True
  19.       Height          =   2535
  20.       Left            =   0
  21.       Picture         =   "ToGray.frx":0000
  22.       ScaleHeight     =   165
  23.       ScaleMode       =   3  'Pixel
  24.       ScaleWidth      =   205
  25.       TabIndex        =   0
  26.       Top             =   0
  27.       Width           =   3135
  28.    End
  29. End
  30. Attribute VB_Name = "ToGrayForm"
  31. Attribute VB_Creatable = False
  32. Attribute VB_Exposed = False
  33. Option Explicit
  34.  
  35. Const SysPalSize = 256
  36. Const StaticColor1 = 9
  37. Const StaticColor2 = 246
  38.  
  39. Dim LogicalPalette As Integer
  40. ' ***********************************************
  41. ' Load the ImagePict palette so its entries
  42. ' match the system entries.
  43. ' ***********************************************
  44. Sub LoadLogicalPalette()
  45. Dim palentry(0 To 255) As PALETTEENTRY
  46. Dim blanked(0 To 255) As PALETTEENTRY
  47. Dim i As Integer
  48.  
  49.     ' Save the logical pallette handle.
  50.     LogicalPalette = ImagePict.Picture.hPal
  51.     
  52.     ' Make sure ImagePict has the foreground palette.
  53.     i = RealizePalette(ImagePict.hdc)
  54.  
  55.     ' Give the system a chance to catch up.
  56.     DoEvents
  57.  
  58.     ' Make the logical palette as big as possible.
  59.     If ResizePalette(LogicalPalette, SysPalSize) = 0 Then
  60.         Beep
  61.         MsgBox "Error resizing logical palette.", _
  62.             vbExclamation
  63.         Exit Sub
  64.     End If
  65.     
  66.     ' Get the system palette entries.
  67.     i = GetSystemPaletteEntries(ImagePict.hdc, 0, SysPalSize, palentry(0))
  68.     
  69.     ' Blank the non-static colors.
  70.     For i = 0 To StaticColor1
  71.         blanked(i) = palentry(i)
  72.     Next i
  73.     For i = StaticColor1 + 1 To StaticColor2 - 1
  74.         With blanked(i)
  75.             .peRed = 0
  76.             .peGreen = 0
  77.             .peBlue = 0
  78.             .peFlags = PC_NOCOLLAPSE
  79.         End With
  80.     Next i
  81.     For i = StaticColor2 To 255
  82.         blanked(i) = palentry(i)
  83.     Next i
  84.     i = SetPaletteEntries(LogicalPalette, 0, SysPalSize, blanked(0))
  85.  
  86.     ' Insert the non-static colors.
  87.     For i = StaticColor1 + 1 To StaticColor2 - 1
  88.         palentry(i).peFlags = PC_NOCOLLAPSE
  89.     Next i
  90.     i = SetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1))
  91.     
  92.     ' Realize the new palette values.
  93.     i = RealizePalette(ImagePict.hdc)
  94. End Sub
  95.  
  96. ' ***********************************************
  97. ' Load the indicated file and prepare to work
  98. ' with its palette.
  99. ' ***********************************************
  100. Sub LoadImagePict(fname As String)
  101.     On Error GoTo LoadFileError
  102.     ImagePict.Picture = LoadPicture(fname)
  103.     Exit Sub
  104.     
  105. LoadFileError:
  106.     Beep
  107.     MsgBox "Error loading file " & fname & "." & _
  108.         vbCrLf & Error$
  109.     Exit Sub
  110. End Sub
  111.  
  112.  
  113. ' ***********************************************
  114. ' 1. Make sure we can handle palettes.
  115. ' 2. Find out how big the system palette is and how
  116. ' many static colors there are.
  117. ' 3. Load and display the system palette.
  118. ' ***********************************************
  119. Private Sub Form_Load()
  120. Dim cmd As String
  121. Dim sp As Integer
  122. Dim infile As String
  123. Dim outfile As String
  124.  
  125.     ' Make sure the screen supports palettes.
  126.     If Not GetDeviceCaps(hdc, RASTERCAPS) And RC_PALETTE Then
  127.         Beep
  128.         MsgBox "This monitor does not support palettes.", _
  129.             vbCritical
  130.         End
  131.     End If
  132.  
  133.     ' Get the input and output file names.
  134.     cmd = Trim$(Command)
  135.     If cmd = "" Then GoTo Usage
  136.     
  137.     sp = InStr(cmd, " ")
  138.     If sp = 0 Then
  139.         infile = cmd
  140.     Else
  141.         infile = Left$(cmd, sp - 1)
  142.         If sp < Len(cmd) Then _
  143.             outfile = Trim$(Mid$(cmd, sp + 1))
  144.     End If
  145.     If outfile = "" Then outfile = infile
  146.         
  147.     ' RealizePalette doesn't work unless the
  148.     ' picture is visible.
  149.     Me.Show
  150.     
  151.     ' Load image, convert, and save the image.
  152.     LoadImagePict infile
  153.     LoadLogicalPalette
  154.     ConvertToGrays
  155.     SaveImagePict outfile
  156.     
  157.     End
  158.     
  159. Usage:
  160.     Beep
  161.     MsgBox "Usage: ToGray infile [outfile]", vbCritical
  162.     End
  163. End Sub
  164.  
  165. ' ***********************************************
  166. ' Save the picture in the indicated file.
  167. ' ***********************************************
  168. Sub SaveImagePict(fname As String)
  169.     On Error GoTo SaveError
  170.     SavePicture ImagePict.Picture, fname
  171.     Exit Sub
  172.  
  173. SaveError:
  174.     Beep
  175.     MsgBox "Error saving picture in file " & _
  176.         fname & "." & vbCrLf & vbCrLf & _
  177.         Error$, , vbExclamation
  178.     Exit Sub
  179. End Sub
  180. ' ***********************************************
  181. ' Replace colors with appropriate grays.
  182. ' ***********************************************
  183. Private Sub ConvertToGrays()
  184. Dim palentry(0 To 255) As PALETTEENTRY
  185. Dim i As Integer
  186. Dim clr As Integer
  187.  
  188.     ' Get the current color values.
  189.     i = GetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1))
  190.  
  191.     ' Fill in the nearest shades.
  192.     For i = StaticColor1 + 1 To StaticColor2 - 1
  193.         With palentry(i)
  194.             clr = (CInt(.peRed) + .peGreen + .peBlue) / 3
  195.             .peRed = clr
  196.             .peGreen = clr
  197.             .peBlue = clr
  198.             .peFlags = PC_NOCOLLAPSE
  199.         End With
  200.     Next i
  201.     If SetPaletteEntries(LogicalPalette, StaticColor1 + 1, StaticColor2 - StaticColor1 - 1, palentry(StaticColor1 + 1)) = 0 Then
  202.         Beep
  203.         MsgBox "Error resetting colors.", , vbExclamation
  204.         Exit Sub
  205.     End If
  206.     i = RealizePalette(ImagePict.hdc)
  207. End Sub
  208.  
  209. ' ************************************************
  210. ' Make the image as big as possible.
  211. ' (This is really only useful during debugging
  212. ' since the form is normally not visible.)
  213. ' ************************************************
  214. Private Sub Form_Resize()
  215.     ImagePict.Move 0, 0, ScaleWidth, ScaleHeight
  216. End Sub
  217.  
  218.  
  219.